import Vue from 'vue'
import Vuex from 'vuex'
import user from './modules/user'
import setting from './modules/setting'
Vue.use(Vuex)

export default new Vuex.Store({
    // 严格模式
    strict: true,
    // 数据初始化
    state: {
        count: 2222,
        name: 'zhengx',
        age: 19,
        list: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
    },
    mutations: {
        //更改数据
        changeHandel(state, obj) {
            state.count += obj.n

        },
        changeHandel2(state, m) {
            state.count += m

        }
    },
    actions: {
        //异步数据
        asyncHadle({ commit }, m) {
            setTimeout(() => {
                console.log(x); //X是一个对象相当于this.$store
                //commit ,调用mutations里面的方法
                commit('changeHandel2', m)
            }, 1000)
        }
        // asyncHadle(x, m) {
        //     setTimeout(() => {
        //         console.log(x); //X是一个对象相当于this.$store
        //         //commit ,调用mutations里面的方法
        //         x.commit('changeHandel2', m)
        //     }, 1000)
        // }
    },
    getters: {
        //类似于computed,用法同state
        total(state) {
            return state.list.reduce((sum, item) => sum + item, 0)
        }
    },
    modules: {
        user,
        setting
    }
})